home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Musique / Quod Libet / quodlibet-3.3.0-installer.exe / bin / shlex.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2014-12-31  |  7KB  |  290 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.7)
  3.  
  4. '''A lexical analyzer class for simple shell-like syntaxes.'''
  5. import os.path as os
  6. import sys
  7. from collections import deque
  8.  
  9. try:
  10.     from cStringIO import StringIO
  11. except ImportError:
  12.     from StringIO import StringIO
  13.  
  14. __all__ = [
  15.     'shlex',
  16.     'split']
  17.  
  18. class shlex:
  19.     '''A lexical analyzer class for simple shell-like syntaxes.'''
  20.     
  21.     def __init__(self, instream = None, infile = None, posix = False):
  22.         if isinstance(instream, basestring):
  23.             instream = StringIO(instream)
  24.         if instream is not None:
  25.             self.instream = instream
  26.             self.infile = infile
  27.         else:
  28.             self.instream = sys.stdin
  29.             self.infile = None
  30.         self.posix = posix
  31.         if posix:
  32.             self.eof = None
  33.         else:
  34.             self.eof = ''
  35.         self.commenters = '#'
  36.         self.wordchars = 'abcdfeghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_'
  37.         if self.posix:
  38.             self.wordchars += '\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd8\xd9\xda\xdb\xdc\xdd\xde'
  39.         self.whitespace = ' \t\r\n'
  40.         self.whitespace_split = False
  41.         self.quotes = '\'"'
  42.         self.escape = '\\'
  43.         self.escapedquotes = '"'
  44.         self.state = ' '
  45.         self.pushback = deque()
  46.         self.lineno = 1
  47.         self.debug = 0
  48.         self.token = ''
  49.         self.filestack = deque()
  50.         self.source = None
  51.         if self.debug:
  52.             print 'shlex: reading from %s, line %d' % (self.instream, self.lineno)
  53.  
  54.     
  55.     def push_token(self, tok):
  56.         '''Push a token onto the stack popped by the get_token method'''
  57.         if self.debug >= 1:
  58.             print 'shlex: pushing token ' + repr(tok)
  59.         self.pushback.appendleft(tok)
  60.  
  61.     
  62.     def push_source(self, newstream, newfile = None):
  63.         """Push an input source onto the lexer's input source stack."""
  64.         if isinstance(newstream, basestring):
  65.             newstream = StringIO(newstream)
  66.         self.filestack.appendleft((self.infile, self.instream, self.lineno))
  67.         self.infile = newfile
  68.         self.instream = newstream
  69.         self.lineno = 1
  70.         if self.debug:
  71.             if newfile is not None:
  72.                 print 'shlex: pushing to file %s' % (self.infile,)
  73.             else:
  74.                 print 'shlex: pushing to stream %s' % (self.instream,)
  75.  
  76.     
  77.     def pop_source(self):
  78.         '''Pop the input source stack.'''
  79.         self.instream.close()
  80.         (self.infile, self.instream, self.lineno) = self.filestack.popleft()
  81.         if self.debug:
  82.             print 'shlex: popping to %s, line %d' % (self.instream, self.lineno)
  83.         self.state = ' '
  84.  
  85.     
  86.     def get_token(self):
  87.         """Get a token from the input stream (or from stack if it's nonempty)"""
  88.         if self.pushback:
  89.             tok = self.pushback.popleft()
  90.             if self.debug >= 1:
  91.                 print 'shlex: popping token ' + repr(tok)
  92.             return tok
  93.         raw = None.read_token()
  94.         if self.source is not None:
  95.             while raw == self.source:
  96.                 spec = self.sourcehook(self.read_token())
  97.                 if spec:
  98.                     (newfile, newstream) = spec
  99.                     self.push_source(newstream, newfile)
  100.                 raw = self.get_token()
  101.         while raw == self.eof:
  102.             if not self.filestack:
  103.                 return self.eof
  104.             None.pop_source()
  105.             raw = self.get_token()
  106.         if self.debug >= 1:
  107.             if raw != self.eof:
  108.                 print 'shlex: token=' + repr(raw)
  109.             else:
  110.                 print 'shlex: token=EOF'
  111.         return raw
  112.  
  113.     
  114.     def read_token(self):
  115.         quoted = False
  116.         escapedstate = ' '
  117.         while True:
  118.             nextchar = self.instream.read(1)
  119.             if nextchar == '\n':
  120.                 self.lineno = self.lineno + 1
  121.             if self.debug >= 3:
  122.                 print 'shlex: in state', repr(self.state), 'I see character:', repr(nextchar)
  123.             if self.state is None:
  124.                 self.token = ''
  125.                 break
  126.                 continue
  127.             if self.state == ' ':
  128.                 if not nextchar:
  129.                     self.state = None
  130.                     break
  131.                 elif nextchar in self.whitespace:
  132.                     if self.debug >= 2:
  133.                         print 'shlex: I see whitespace in whitespace state'
  134.                     if not self.token:
  135.                         if self.posix and quoted:
  136.                             break
  137.                         
  138.                     
  139.                 if nextchar in self.commenters:
  140.                     self.instream.readline()
  141.                     self.lineno = self.lineno + 1
  142.                 elif self.posix and nextchar in self.escape:
  143.                     escapedstate = 'a'
  144.                     self.state = nextchar
  145.                 elif nextchar in self.wordchars:
  146.                     self.token = nextchar
  147.                     self.state = 'a'
  148.                 elif nextchar in self.quotes:
  149.                     if not self.posix:
  150.                         self.token = nextchar
  151.                     self.state = nextchar
  152.                 elif self.whitespace_split:
  153.                     self.token = nextchar
  154.                     self.state = 'a'
  155.                 else:
  156.                     self.token = nextchar
  157.                     if not self.token:
  158.                         if self.posix and quoted:
  159.                             break
  160.                         
  161.                         continue
  162.                         if self.state in self.quotes:
  163.                             quoted = True
  164.                             if not nextchar:
  165.                                 if self.debug >= 2:
  166.                                     print 'shlex: I see EOF in quotes state'
  167.                                 raise ValueError, 'No closing quotation'
  168.                             if nextchar == self.state:
  169.                                 if not self.posix:
  170.                                     self.token = self.token + nextchar
  171.                                     self.state = ' '
  172.                                     break
  173.                                 else:
  174.                                     self.state = 'a'
  175.                             elif self.posix and nextchar in self.escape and self.state in self.escapedquotes:
  176.                                 escapedstate = self.state
  177.                                 self.state = nextchar
  178.                             else:
  179.                                 self.token = self.token + nextchar
  180.             if self.state in self.escape:
  181.                 if not nextchar:
  182.                     if self.debug >= 2:
  183.                         print 'shlex: I see EOF in escape state'
  184.                     raise ValueError, 'No escaped character'
  185.                 if escapedstate in self.quotes and nextchar != self.state and nextchar != escapedstate:
  186.                     self.token = self.token + self.state
  187.                 self.token = self.token + nextchar
  188.                 self.state = escapedstate
  189.                 continue
  190.             if not self.state == 'a' or nextchar:
  191.                 self.state = None
  192.                 break
  193.             elif nextchar in self.whitespace:
  194.                 if self.debug >= 2:
  195.                     print 'shlex: I see whitespace in word state'
  196.                 self.state = ' '
  197.                 if not self.token:
  198.                     if self.posix and quoted:
  199.                         break
  200.                     
  201.                 
  202.             if nextchar in self.commenters:
  203.                 self.instream.readline()
  204.                 self.lineno = self.lineno + 1
  205.                 if self.posix:
  206.                     self.state = ' '
  207.                     if not self.token:
  208.                         if self.posix and quoted:
  209.                             break
  210.                         
  211.                     
  212.                 
  213.             if self.posix and nextchar in self.quotes:
  214.                 self.state = nextchar
  215.             elif self.posix and nextchar in self.escape:
  216.                 escapedstate = 'a'
  217.                 self.state = nextchar
  218.             elif nextchar in self.wordchars and nextchar in self.quotes or self.whitespace_split:
  219.                 self.token = self.token + nextchar
  220.             else:
  221.                 self.pushback.appendleft(nextchar)
  222.                 if self.debug >= 2:
  223.                     print 'shlex: I see punctuation in word state'
  224.                 self.state = ' '
  225.                 if self.token:
  226.                     break
  227.                 
  228.         result = self.token
  229.         self.token = ''
  230.         if self.posix and not quoted and result == '':
  231.             result = None
  232.         if self.debug > 1:
  233.             if result:
  234.                 print 'shlex: raw token=' + repr(result)
  235.             else:
  236.                 print 'shlex: raw token=EOF'
  237.         return result
  238.  
  239.     
  240.     def sourcehook(self, newfile):
  241.         '''Hook called on a filename to be sourced.'''
  242.         if newfile[0] == '"':
  243.             newfile = newfile[1:-1]
  244.         if isinstance(self.infile, basestring) and not os.path.isabs(newfile):
  245.             newfile = os.path.join(os.path.dirname(self.infile), newfile)
  246.         return (newfile, open(newfile, 'r'))
  247.  
  248.     
  249.     def error_leader(self, infile = None, lineno = None):
  250.         '''Emit a C-compiler-like, Emacs-friendly error-message leader.'''
  251.         if infile is None:
  252.             infile = self.infile
  253.         if lineno is None:
  254.             lineno = self.lineno
  255.         return '"%s", line %d: ' % (infile, lineno)
  256.  
  257.     
  258.     def __iter__(self):
  259.         return self
  260.  
  261.     
  262.     def next(self):
  263.         token = self.get_token()
  264.         if token == self.eof:
  265.             raise StopIteration
  266.         return token
  267.  
  268.  
  269.  
  270. def split(s, comments = False, posix = True):
  271.     lex = shlex(s, posix = posix)
  272.     lex.whitespace_split = True
  273.     if not comments:
  274.         lex.commenters = ''
  275.     return list(lex)
  276.  
  277. if __name__ == '__main__':
  278.     if len(sys.argv) == 1:
  279.         lexer = shlex()
  280.     else:
  281.         file = sys.argv[1]
  282.         lexer = shlex(open(file), file)
  283.     while None:
  284.         tt = lexer.get_token()
  285.         if tt:
  286.             print 'Token: ' + repr(tt)
  287.             continue
  288.         break
  289.         continue
  290.